using System;
using System.Collections.Generic;
using UnityEngine;

public class BattleUIViewController : ViewController,IBattleMainUI
{
    public BattleSceneController battleScene => GetComponentInParent<BattleSceneController>();
    
    BattleMainUI m_Panel;
    public BattleMainUI panel => m_Panel;

    
    public BattleMainUI Init(BattleController battleController)
    {
        if (m_Panel == null)
            m_Panel = InitBattleUI();
        try
        {
            m_Panel.Init(battleController);
        }
        catch (Exception e)
        {
            GameDebug.LogException(e);
        }
        return m_Panel;
    }
    
    protected override void WillLoadView()
    {
        base.WillLoadView();

    }

    protected virtual BattleMainUI InitBattleUI()
    {
        BattleMainUI prefab = ResourceManager.Load<BattleMainUI>(ViewPath.BattlePanel);
        if (prefab == null)
        {
            GameDebug.LogError("battle ui prefab null");
            return null;
        }
        m_Panel = GameObject.Instantiate<BattleMainUI>(prefab,transform);
        m_Panel.uiListener = this;
        m_Panel.SetViewController(this);
       // panel.name = "战斗UI";
        return m_Panel;
    }

    #region  UI implement

    public virtual void OnClickGoBack()
    {
        Game.Instance.LoadCurLevelGame();
    }

    public void OnClickRestart()
    {
        Game.Instance.LoadGame(battleScene.battleController.level,battleScene.battleController.mapId);
    }

    public virtual void OnClickLevelList()
    {
        GameDebug.Log("OnClickLevelList");

        var mapList = battleScene.battleSceneNav.LoadDisplay<MapListDisplayController>(null, false);
        MapListPanelData mapListPanelData = new MapListPanelData();
        
        mapListPanelData.itemDatas = new List<MapListItemData>(10);
        //for (int i = 1; i <= Game.maxLevel; i++)
        //{
        //    if (i <= Game.Instance.user.level)
        //    {
        //        mapListPanelData.itemDatas.Add(new MapListItemData()
        //        {
        //            level = i,
        //            active = true,
        //            star = Game.Instance.user.GetStar(i)
        //        });
        //    }
        //    else
        //    {
        //        mapListPanelData.itemDatas.Add(new MapListItemData()
        //        {
        //            level = i,
        //            active = false,
        //            star = Game.Instance.user.GetStar(i)
        //        });
        //    }
        //}
        
        mapList.Init(mapListPanelData);
    }

    public void OnClickAdSkipLevel()
    {
#if UNITY_EDITOR
        OnWatchAdEnd();
        return;
#endif
        //AudioController.instance.PauseMusic();

        //Game.Instance.adManager.PlayVideoAd(() =>
        //{
        //    AudioController.instance.ResumeMusic();

        //    Game.Instance.LoadNextLevelGame();
        //}, () =>
        //{
        //    AudioController.instance.ResumeMusic();
        //}
        //, (err) =>
        //{
        //    AudioController.instance.ResumeMusic();
        //    Game.Instance.ShowNoticePanel(err);
        //});
    }

    public void OnClickSetting()
    {
        parentNav.LoadDisplay<GameSettingViewController>(null, false);
    }

    void OnWatchAdEnd()
    {
        GameDebug.Log("跳到下一关");
       
        Game.Instance.NextLevel();   
        Game.Instance.LoadCurLevelGame();
    }

    #endregion

    public override void WillHideView()
    {
        base.WillHideView();

        if (this.panel)
        {
            this.panel.PlayHideAnim();
        }
    }

    public override void OnViewDelete()
    {
        base.OnViewDelete();

        if (m_Panel)
        {
            Destroy(m_Panel.gameObject);
        }
    }

    private void OnDestroy()
    {
        
    }


}